/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.usercenter.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.dto.UserDTO;
import cn.hallele.mc.sdk.usercenter.dto.UserQueryDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.usercenter.application.action.user.UserApplicationService;
import cn.hallele.ms.usercenter.application.bo.UserBO;
import cn.hallele.ms.usercenter.application.bo.UserQueryBO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/10/5 16:15
 */
@Slf4j
@Service
public class UserDBDas extends ModelDas<UserApplicationService, UserDTO, UserBO> {
    protected UserDBDas(UserApplicationService option) {
        super(option);
    }

    /**
     * 条件分页查询用户
     *
     * @param condition 查询条件
     * @return 用户
     */
    public Page<UserDTO> listUser(@Valid UserQueryDTO condition) {
        return revertPage(this.option.listUser(convert(condition)));
    }

    /**
     * 获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    public UserDTO getUser(String username) {
        return revert(this.option.getUser(username));

    }

    /**
     * 添加用户
     *
     * @param userDTO 用户信息
     */
    public void addUser(@Valid UserDTO userDTO) {
        this.option.addUser(convert(userDTO));
    }

    /**
     * 修改用户
     *
     * @param userDTO 用户
     * @return 修改后的用户信息
     */
    public UserDTO updateUser(@Valid UserDTO userDTO) {
        return revert(this.option.updateUser(convert(userDTO)));
    }

    /**
     * 删除用户
     *
     * @param username 用户名
     */
    public void deleteUser(String username) {
        this.option.deleteUser(username);
    }

    /**
     * 修改密码
     *
     * @param username    用户名
     * @param oldPassword 旧密码，加密的
     * @param newPassword 新密码，加密的
     * @return true：成功， false：失败
     */
    public Boolean resetPassword(String username, String oldPassword, String newPassword) {
        return this.option.resetPassword(username, oldPassword, newPassword);
    }

    /**
     * 获取用户头像
     *
     * @param username 用户名
     * @return 用户头像
     */
    public String getAvatar(String username) {
        return this.option.getAvatar(username);
    }

    /**
     * 将T转成E
     *
     * @param userDTO 被转换者
     * @return S 转换后者
     */
    @Override
    protected UserBO convert(UserDTO userDTO) {
        return ObjectUtil.map(userDTO, u -> UserBO.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .password(u.getPassword())
                .avatar(u.getAvatar())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(u.getSex())
                .age(u.getAge())
                .status(u.getStatus())
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param userBO 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected UserDTO revert(UserBO userBO) {
        return ObjectUtil.map(userBO, u -> UserDTO.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .username(u.getUsername())
                .password(u.getPassword())
                .avatar(u.getAvatar())
                .phoneNumber(u.getPhoneNumber())
                .email(u.getEmail())
                .sex(u.getSex())
                .age(u.getAge())
                .status(u.getStatus())
                .personalitySignature(u.getPersonalitySignature())
                .passExpiredTime(u.getPassExpiredTime())
                .accountExpiredTime(u.getAccountExpiredTime())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .createBy(u.getCreateBy())
                .updateBy(u.getUpdateBy())
                .build());
    }

    /**
     * 转换UserQueryDTO -> UserQueryBO
     *
     * @param userQueryDTO UserQueryDTO
     * @return UserQueryBO
     */
    private UserQueryBO convert(UserQueryDTO userQueryDTO) {
        return ObjectUtil.map(userQueryDTO, u -> UserQueryBO.builder()
                .id(u.getId())
                .tenantId(u.getTenantId())
                .deptId(u.getDeptId())
                .username(u.getUsername())
                .phoneNumber(u.getPhoneNumber())
                .status(u.getStatus())
                .keywords(u.getKeywords())
                .pageNo(u.getPageNo())
                .pageSize(u.getPageSize())
                .startTime(u.getStartTime())
                .endTime(u.getEndTime())
                .build());
    }
}
